const mysql = require('../../mysql')
const formatDate = require('../../utils/formatDate')

async function get_template_name(ctx) {
  const name_list = await mysql('template')
                          .select()

  ctx.state.data = {
    name_list
  }
}

async function get_dispensable_parameter(ctx) {
  const { template_id } = ctx.request.body

  const list = await mysql('dispensable_parameter')
                     .where({ template_id })
  ctx.state.data={
    list
  }
}

async function add_goods(ctx) {
  const { goods, parameter} = ctx.request.body
  try {
    var id = await mysql('goods')
                  .insert(goods)
    for (let i=0; i<parameter.length; i++) {
      parameter[i].goods_id = id
    }
    await mysql('goods_dispensable_parameter')
    .insert(parameter)

    ctx.state.data = {
      msg: '修改成功'
    }
  } catch (err) {
    ctx.state = {
      code: -1,
      data: {
        msg: err
      }
    }
  }
}

async function updata_goods(ctx) {
  const { goods, parameter} = ctx.request.body
  try {
    // 更新商品
    await mysql('goods')
    .where("id", goods.id)
    .update({
      "goods_name": goods.goods_name,
      "serial_number": goods.serial_number,
      "state": goods.state,
      "comment": goods.comment,
    })
    await mysql('goods_dispensable_parameter')
          .where("goods_id", goods.id)
          .del()
    for (var i=0; i<parameter.length; i++) {
      parameter[i].goods_id = goods.id
    }
    await mysql('goods_dispensable_parameter')
          .insert(parameter)

    ctx.state.data = {
      msg: '修改成功'
    }
  } catch (err) {
    ctx.state = {
      code: -1,
      data: {
        msg: err
      }
    }
  }
}

async function get_goods(ctx){
  const { goods_name, state, page, page_size } = ctx.request.body

  const goods_list = await mysql('goods')
                            .where('goods_name', 'like', `%${goods_name}%`)
                            .andWhere('state', 'like', `%${state}%`)
                            .limit(Number(page_size))
                            .offset((Number(page) - 1) * Number(page_size))
                            .orderBy('id', 'desc')
                            .select()
  const total = await mysql('goods')
                      .where('goods_name', 'like', `%${goods_name}%`)
                      .andWhere('state', 'like', `%${state}%`)
                      .select()
  ctx.state.data = {
    goods_list,
    total: total.length
  }

}

async function goods_sold(ctx) {
  const { data } = ctx.request.body

  try {
    const time = formatDate(new Date, 'yyyy-MM-dd')
    data.selling_time = time
    await mysql('goods_sold')
          .insert(data)

    await mysql('goods')
          .where({
            id: data.goods_id
          })
          .update({
            state: 2
          })
    ctx.state.data = {
      msg: '售出成功'
    }
  } catch (err) {
    ctx.state = {
      code: -1,
      data: {
        msg: err
      }
    }
  }

}

async function get_goods_parameter(ctx) {
  const { goods_id } = ctx.request.body
  
  const parameter = await mysql('goods_dispensable_parameter')
                          .where({goods_id})

  ctx.state.data = {
    parameter
  }
}

async function validate_serial_number(ctx) {
  const { serial_number, id } = ctx.request.body

  const goods = await mysql('goods')
                      .where({
                        serial_number
                      })
                      .select()
  let state
  if (goods.length) {
    if (goods[0].id === id){
      state = false
    } else {
      state = true
    }
  } else {
    state = false
  }                    
  ctx.state.data = {
    state
  }                    
}


module.exports = {
  get_template_name,
  get_dispensable_parameter,
  add_goods,
  updata_goods,
  get_goods,
  goods_sold,
  get_goods_parameter,
  validate_serial_number
}